function checktoken() {
    return async function (ctx, next) {
        try {
            const token = ctx.request.header.token;
            if (!token) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: '未提供认证token'
                }
                return;
            }

            let decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            // 增强验证，支持多种用户标识
            if (decode.user_id || decode.admin_id) {
                ctx.state = {
                    username: decode.username || decode.email,
                    user_id: decode.user_id,
                    admin_id: decode.admin_id,
                    type: decode.type || 'user' // 默认为用户类型
                }
                await next();
            } else {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: 'token验证失败'
                }
            }
        } catch (error) {
            console.log(error);
            ctx.body = {
                code: 1,
                data: null,
                msg: 'token验证发生异常'
            }
        }
    }
}

module.exports = checktoken;